/*
 * Copyright (C) 2005 Dmitri Maximovich <maxim@md.pp.ru>
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 */

// upgrade script
// this variable getting replaced in build script
const ccVersion = "@version";

// al upgrades action goes here
function ccAfterInstall() {
	ccLog("Running afterInstall()");

	try {
		// delete unused properties
		var prefService = getPrefService();
		if (prefService.prefHasUserValue("lastUpdate")) {
			prefService.deleteBranch("lastUpdate");
		}
		if (prefService.prefHasUserValue("lastUpdateInt")) {
			prefService.deleteBranch("lastUpdateInt");
		}
	}
	catch (e) {
		ccLog("Error in afterInstall(): "+e);
	}
}

function ccPing(prevVersion) {
  try {
	  var request = new XMLHttpRequest();
	  // encodeURIComponent(window.navigator.appVersion)
	  request.open("GET", "http://www.md.pp.ru/mozilla/cc/ping.jsp?fromVersion="+prevVersion+"&toVersion="+ccVersion, true);
	  request.send(null);
    }
	catch (e) {
		ccLog("Error in ping(): "+e);
	}
}

function ccUpdate() {
	try {
		// check for version
		const prevVersion = getCcVersion();
		if (ccVersion == prevVersion) {
			return;
		}
	
        ccLog("ccUpdate - running");
		ccPing(prevVersion);

		// update version
		setCcVersion(ccVersion);
	
		// this will run only once on install/upgrade of plugin
		ccAfterInstall();

        ccLog("done");
    }
	catch (e) {
		ccLog("Error in ccUpdate(): "+e);
	}
}

function getCcVersion() {
	return getPrefService().getCharPref("version");
}

function setCcVersion(aValue) {
    getPrefService().setCharPref("version", aValue);
}

// run
window.addEventListener("load", function() {ccUpdate();}, false);
